package org.linitly;

import org.dom4j.DocumentException;
import org.linitly.util.FileCopyUtil;

import java.io.File;
import java.io.IOException;

/**
 * @author: linxiunan
 * @date: 2021/5/28 16:59
 * @descrption:
 */
public class FileCreate {

    private boolean updateJava;
    private boolean updateVue;
    private boolean updateReact;

    public FileCreate(boolean updateJava, boolean updateVue, boolean updateReact) {
        this.updateJava = updateJava;
        this.updateVue = updateVue;
        this.updateReact = updateReact;
    }


    private static final String DEFAULT_JAVA_PROJECT_NAME = "linitly-boot";
    private static final String DEFAULT_VUE_PROJECT_NAME = "linitly-vue";
    private static final String DEFAULT_REACT_PROJECT_NAME = "linitly-react-antd";

    private static final String DEFAULT_TOOLS_DIR_NAME = "tools";
    private static final String NODE_MODULES_DIR_NAME = "node_modules";


    public void create(String projectName, String packageName, File projectPathFile, File newProjectPathDir) throws IOException, DocumentException {
        File[] listFiles = projectPathFile.listFiles();

        if (listFiles == null || listFiles.length < 1) {
            System.out.println("project path error, no file under this path");
            System.exit(-1);
            return;
        }
        for (int i = 0; i < listFiles.length; i++) {
            if (listFiles[i].isDirectory()) {
//                File newJavaProjectFile = dealDirectory(projectPathFile, projectName);
//                JavaProjectDeal.update(projectName, packageName, newJavaProjectFile, beforePackageName);
                boolean isProjectDir = isProjectDir(listFiles[i]);
                if (!isProjectDir) {
                    copyDir(listFiles[i], newProjectPathDir);
                    continue;
                }
                if (isJavaDir(listFiles[i].getName())) {
                    JavaProjectDeal.deal(projectName, packageName, listFiles[i], newProjectPathDir);
                }
                // 现在只有react，目前没有像java一样形成比较好的框架，暂时只复制所有内容
                if (isReactDir(listFiles[i].getName())) {
                    copyDir(listFiles[i], newProjectPathDir);
                }
                // TODO 其它文件
            } else if (listFiles[i].isFile()) {
                // 复制第一层次的文件
                FileCopyUtil.copyFile(listFiles[i], newProjectPathDir);
            }
        }
    }

    /**
     * @author Linitly
     * @date 16:07 2022/3/10
     * @description 循环遍历复制某一个文件夹
     */
    private void copyDir(File dir, File newProjectPathDir) throws IOException {
        // 如果是工具目录或者node_modules目录，不处理
        // 至于其它的文件：.iml log logs target等等文件，现在不处理，对于真实的用户其实是不希望这样使用的
        if (isToolsDir(dir.getName()) || isNodeModulesDir(dir.getName())) return;
        File destDir = FileCopyUtil.copyDir(dir, newProjectPathDir);
        File[] listFiles = dir.listFiles();
        if (listFiles == null || listFiles.length < 1) return;
        for (int i = 0; i < listFiles.length; i++) {
            if (listFiles[i].isDirectory()) {
                copyDir(listFiles[i], destDir);
            } else if (listFiles[i].isFile()) {
                FileCopyUtil.copyFile(listFiles[i], destDir);
            }
        }
    }

    private boolean isProjectDir(File dirFile) {
        String dirFileName = dirFile.getName();
        return isJavaDir(dirFileName) || isVueDir(dirFileName) || isReactDir(dirFileName);
    }

    private boolean isJavaDir(String dirName) {
        return dirName.equals(DEFAULT_JAVA_PROJECT_NAME);
    }

    private boolean isVueDir(String dirName) {
        return dirName.equals(DEFAULT_VUE_PROJECT_NAME);
    }

    private boolean isReactDir(String dirName) {
        return dirName.equals(DEFAULT_REACT_PROJECT_NAME);
    }

    private boolean isToolsDir(String dirName) {
        return dirName.equals(DEFAULT_TOOLS_DIR_NAME);
    }

    private boolean isNodeModulesDir(String dirName) {
        return dirName.equals(NODE_MODULES_DIR_NAME);
    }
}
